from flask import Flask, render_template, request, redirect, url_for
import os
import sqlite3

app = Flask(__name__)

DATABASE = 'FishModel.db'

def get_db_connection():
    conn = sqlite3.connect(DATABASE)
    conn.row_factory = sqlite3.Row
    return conn

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # 处理文件上传
        file = request.files['file']
        file_path = os.path.join('uploads', file.filename)
        file.save(file_path)

        # 将文件信息保存到数据库
        conn = get_db_connection()
        c = conn.cursor()
        c.execute("INSERT INTO DATA (name,path) VALUES (?,?)", (file.filename, file_path))
        conn.commit()
        conn.close()

        conn = get_db_connection()
        data = conn.execute('SELECT * FROM DATA').fetchall()
        conn.close()

        return render_template('test555.html', data=data)

    return render_template('test555.html')

if __name__ == '__main__':
    app.run(debug=True)